﻿using System;
using System.Collections.Generic;
using System.IO;
using Authority.Logic;
using Common.Library;
using Document.Library;
using Container.Library;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Project.Logic;
using Project.Model;
using Tool.Model;
using Web.Manage.App_Start;

namespace Web.Manage.Areas.Staff.Controllers
{
    /// <summary>
    /// 员工管理控制器
    /// </summary>
    [Area("Staff")]
    public class EmployeeController : BaseController
    {

        private readonly IEmployeeService employeeService = null;//员工管理逻辑
        private IHostingEnvironment hostingEnv;

        public EmployeeController(IHostingEnvironment env)
        {
            this.hostingEnv = env;
            //员工管理逻辑注入
            employeeService = UnityCIContainer.Instance.GetService<IEmployeeService>();
        }


        #region 界面

        [UserAuthorization(true, FunctionValidateType.VerifyView)]
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 编辑界面
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult Modify()
        {
            return View();
        }

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult UploadImg()
        {
            return View();
        }
        #endregion

        #region 查询

        /// <summary>
        /// 获取员工分页列表
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult LoadEmployeeList()
        {
            var resultInfo = new ResultJsonInfo<List<T_EmployeeEntity>>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<EmployeeInfoQueryRequest>>();
              
                resultInfo = employeeService.LoadEmployeeList(parametersInfo);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Msg = ex.Message;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }


        /// <summary>
        /// 获取员工图片信息
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult LoadEmployeeImg()
        {
            var resultInfo = new ResultJsonInfo<T_EmployeeImgEntity>();
            try
            {
                var parametersInfo = DeserializeRequestData<ParametersInfo<string>>();
             
                resultInfo = employeeService.LoadEmployeeImg(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Msg = ex.Message;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }

        /// <summary>
        /// 导出报表
        /// </summary>
        /// <returns>处理结果</returns>
        [EnableCors("AllowSpecificOrigin")]
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult EpxortEmployee()
        {
            var resultInfo = new ResultJsonInfo<List<T_EmployeeEntity>>();

            FileStream fs = null;
            BinaryWriter bw = null;

            try
            {
                var parametersInfo = DeserializeRequestData<ParametersInfo<EmployeeInfoQueryRequest>>();

                resultInfo = employeeService.LoadEmployeeAllList(parametersInfo);

                List<string> headlist = new List<string>()
                {
                   "员工姓名","性别","所在项目","所属机构","职务","工种","信用等级",
                   "联系电话","身份证号","开户银行","银行账户","家庭住址","亲属姓名","联系电话",
                   "是否在职","员工类别","每日工资","小时工资","基本工资","工位工资","社保缴费","公积金","备注"
                };
                string fileName = "员工基础信息-" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";

                var savePath = ExcelHelper.ExportToExcelReturnFilePath<T_EmployeeEntity>(resultInfo.Data, headlist, rowData =>
                {
                    return new object[] {
                                           rowData.Name,
                                           EnumExtentions.GetEnumDescriptionByValue(int.Parse(rowData.Sex),typeof(SexType))   ,
                                           rowData.ProjectName,
                                           rowData.OrgaizeName,
                                           rowData.Post,
                                           rowData.WorkType,
                                           EnumExtentions.GetEnumDescriptionByValue(int.Parse(rowData.Credit),typeof(CreditType))   ,
                                           rowData.Phone,
                                           rowData.No,
                                           rowData.BankName,
                                           rowData.BankName,
                                           rowData.Address,
                                           rowData.CrashPerson,
                                           rowData.CrashPhone,
                                           rowData.ZTBS==true?"在职":"离职",
                                           rowData.MName,
                                           rowData.DayMoney,
                                           rowData.OverMoney,
                                           rowData.MonthMoney,
                                           rowData.PostMoney,
                                           rowData.SocialMoney,
                                           rowData.HouseMoney,
                                           rowData.Descrption
                           };
                },
                false,
                hostingEnv.WebRootPath,
                fileName
                );
                resultInfo.Msg = savePath;
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Msg = ex.Message;
                resultInfo.Excep = ex;
            }
            finally
            {
                if (fs != null) fs.Close();
                if (bw != null) bw.Close();
            }
            return ReturnContent(resultInfo);
        }

        #endregion

        #region 更新操作

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <returns></returns>
        [EnableCors("AllowSpecificOrigin")]
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult UploadImage()
        {
            var resultInfo = new ResultJsonInfo<string>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<Dictionary<string, object>>>();

                resultInfo = employeeService.UploadImage(hostingEnv.WebRootPath, parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Msg = ex.Message;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }

        /// <summary>
        ///修改信息
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult ModifyInfo()
        {
            var resultInfo = new ResultJsonInfo<int>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<EmployeeOperateRequest>>();

                resultInfo = employeeService.Save(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Msg = ex.Message;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }


        /// <summary>
        ///在职与离职操作
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult Forbidden()
        {
            var resultInfo = new ResultJsonInfo<int>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<int>>();

                resultInfo = employeeService.Forbidden(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Msg = ex.Message;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }
        #endregion

        #region 删除信息
        /// <summary>
        /// 删除信息
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult Remove()
        {
            var resultInfo = new ResultJsonInfo<int>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<List<int>>>();

                resultInfo = employeeService.Remove(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Msg = ex.Message;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }
        #endregion
    }
}